package com.luoran.bartalks.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.MessageFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 * 根据经纬度调用百度地图API获取地名
 * 
 * @author ghl
 * @modify M1nG
 */
public class MapGeoUtil {

	/**
	 * 根据经纬度获取地名百度接口
	 * 
	 * @param log
	 * @param lat
	 * @return
	 */
	public static String getAddress(String log, String lat) {
		String sUrl = "http://api.map.baidu.com/geocoder/v2/?ak=8LYlMGDtT2j2FoqYztiuxt5Ki7IHfDTO&location=" + lat + ","
				+ log + "&output=json&pois=0";
		String address = "";
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL(sUrl);
			BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			br.close();
			if (StringUtils.isEmpty(sb.toString())) {
				System.err.println("百度服务无返回");
				return address;
			}
			JSONObject res = null;
			try {
				res = JSONObject.parseObject(sb.toString());// json化接口返回数据
				address = JSONObject.parseObject(res.getString("result")).getString("formatted_address");
			} catch (Exception e) {
				e.printStackTrace();
				return address;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return address;
		}
		return address;
	}

	public static String getGoogleAddres(String lng, String lat) {
		String addr = "";
		if (null == lat || null == lng) {
			return addr;
		}
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// 创建httpget.
			HttpGet httpget = new HttpGet(MessageFormat.format(
					"https://maps.google.com/maps/api/geocode/json?latlng={1},{0}&sensor=false&&language=en&key=AIzaSyDI3Ds0YD1Qt16JQfO6sSvOPCsR-tNJBP4",
					lat, lng));
			System.out.println("executing request " + httpget.getURI());
			// 执行get请求.
			CloseableHttpResponse response = httpclient.execute(httpget);

			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				// 打印响应状态
				System.out.println(response.getStatusLine());
				if (entity != null) {
					// 打印响应内容
					String str = EntityUtils.toString(entity);
					JSONObject o = (JSONObject) JSON.parse(str);
					JSONArray o1 = (JSONArray) o.get("results");

					JSONObject o2 = (JSONObject) o1.get(0);
					if (null != o2) {
						addr = String.valueOf(o2.get("formatted_address"));
						System.out.println("详细地址====>>>" + addr);
						JSONArray o3 = (JSONArray) o2.get("addressComponent");
						System.out.println("地址明细====>>>" + JSONArray.toJSONString(o3));
					}
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return addr;
	}

}
